class csvMgr {
    public constructor() {
	}

    public static txt:any = {}
    public static datas:any = {};               //保存的map    {{}}
    public static dataList:any = {};            //保存的数组    {[]}
    private static limit = {
        delim : ',',//default
        quote : '"',//default
        rowdelim : '\r\n',//windows7x64下excle2010导出csv的换行符是'\r\n'
        comment : '#'//以#开头的行会被忽略(note:仅对bindColumns读取方法进行测试)
    };

    public static parse(csvPath:string, data:any = null):void {
        let text = csvMgr.txt[csvPath];
        if (csvMgr.datas[csvPath]) {
            return;
        }
        
        if (text) {
            let obj = CSV.bindColumns(CSV.parse(text, csvMgr.limit), null);
            csvMgr.datas[csvPath] = obj[0];                 //保存的map
            csvMgr.dataList[csvPath] = obj[1];              //保存的数组
        }
    };

    /**
     * 返回数据对象  object类型
     */
    public static getCsvData(csvPath:string):any {
        let data = csvMgr.datas[csvPath];
        if (!data) {
            csvMgr.parse(csvPath);
        }

        if (!csvMgr.datas[csvPath]) {
            console.log("Error: 不存在配置表" +  csvPath);
            return null;
        }

        return csvMgr.datas[csvPath];
    };

    /**
     * 返回配置数据对象数组  array类型
     */
    public static getCsvDataArray(csvPath:string):any {
        let list = csvMgr.dataList[csvPath];
        if (!list) {
            csvMgr.parse(csvPath);
        }

        if (!csvMgr.dataList[csvPath]) {
            console.log("Error: 不存在配置表" +  csvPath);
            return null;
        }

        return csvMgr.dataList[csvPath];
    };

    /**
     * 仅返回第一个符合要求的对象
     * Usage:
     * csvMgr.getDataBy(csvPaths.csvTest, {id:212}
     * csvMgr.getDataBy(csvPaths.csvTest, {id:212, name:'k'}
     * csvMgr.getDataBy(csvPaths.csvTest, {id:212, level:10}
     * csvMgr.getDataBy(csvPaths.csvTest, {id:212, name:'k', level:10}
     */
    public static getDataBy(csvPath:string, obj:any):any {
        var datas = csvMgr.getCsvData(csvPath);
        for (let dKey in datas) {
            var data = datas[dKey];
            let flag = true;
            for (var key in obj) {
                if (data[key] != obj[key]) {
                    flag = false;
                    break;
                }
            }

            if (!flag) {
                continue;
            } else {
                return data;
            }
        }
        return null;
    };

    /**
     * 根据ID获得配置
     */
    public static getDataById(csvPath:string, obj:any, showLog:boolean = true):any {
        var id = obj.Id;
        var csvId = id + "";
        var data = csvMgr.getCsvData(csvPath);
        if (!data[csvId] && showLog) {
            console.log(csvPath + ":不存在" + csvId);
        }

        let result = null;
        if (data && data[csvId]) {
            result = data[csvId];
        }

        return result;
    }

        /**
     * 获得数据长度
     * @param {csvPaths} csvPath
     * @returns {length|*|Number|length|length|length}
     */
    public static getDataLen(csvPath:string):number {
        var data = csvMgr.getCsvData(csvPath);
        return (data != null) ? Object.keys(data).length : 0;
    };

    /**
     * 获得值相等的所有数据，有遍历数组，表格越大花销越高
     * @param {csvPaths} csvPath
     * @param {JSON} obj
     * @returns {Array}
     */
    public static getDataArr(csvPath:string, obj:any):any {
        var list = [];
        var datas = csvMgr.getCsvData(csvPath);
        for (let dKey in datas) {
            var data = datas[dKey];

            let flag = true;
            for (var key in obj) {
                if (data[key] != obj[key]) {
                    flag = false;
                    break;
                }
            }
            if (!flag) {
                continue;
            } else {
                list.push(data);
            }
        }
        return list;
    }
}

